package com.huaxonline.boot.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    private static Logger cLogger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * 获取现在时间
     *
     * @return 返回时间类型 yyyyMMddHHmm
     */
    public static String getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static Date getNowDate1() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        //		ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = null;
        try {
            currentTime_2 = formatter.parse(dateString);
        } catch (ParseException e) {
            cLogger.error("获取当前时间转换异常");
        }
        return currentTime_2;

    }

    public static String getNowDate_YYYY_MM_DD() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取时间戳后五位
     *
     * @return
     */
    public static String getTime5() {
        String timeStr = System.currentTimeMillis() + "";
        int len = timeStr.length();
        timeStr = timeStr.substring(len - 5, len);
        return timeStr;
    }

    public static String date10to8(String pDate) {
        if ((null == pDate) || ("".equals(pDate))) {
            return pDate;
        }

        return pDate.substring(0, 4) + pDate.substring(5, 7) + pDate.substring(8);
    }

    public static String time8to6(String pTime) {
        if ((null == pTime) || ("".equals(pTime))) {
            return pTime;
        }
        for (int i = pTime.length(); i < 8; i++) {
            pTime = '0' + pTime;
        }

        return pTime.substring(0, 2) + pTime.substring(3, 5) + pTime.substring(6);
    }

    public static String get10Date(Calendar pCalendar) {
        StringBuilder mStrBuilder = new StringBuilder().append(pCalendar.get(1)).append('-');
        int mTempInt = pCalendar.get(2);
        mTempInt++;
        if (mTempInt < 10) {
            mStrBuilder.append('0');
        }
        mStrBuilder.append(mTempInt).append('-');
        mTempInt = pCalendar.get(5);
        if (mTempInt < 10) {
            mStrBuilder.append('0');
        }
        mStrBuilder.append(mTempInt);
        return mStrBuilder.toString();
    }

    public static String getCur10Date() {
        return get10Date(Calendar.getInstance());
    }

    public static String get8Time(Calendar pCalendar) {
        StringBuilder mStrBuilder = new StringBuilder();
        int mTempInt = pCalendar.get(11);
        if (mTempInt < 10) {
            mStrBuilder.append('0');
        }
        mStrBuilder.append(mTempInt).append(':');
        mTempInt = pCalendar.get(12);
        if (mTempInt < 10) {
            mStrBuilder.append('0');
        }
        mStrBuilder.append(mTempInt).append(':');
        mTempInt = pCalendar.get(13);
        if (mTempInt < 10) {
            mStrBuilder.append('0');
        }
        mStrBuilder.append(mTempInt);
        return mStrBuilder.toString();
    }

    public static String getCur8Time() {
        return get8Time(Calendar.getInstance());
    }

    public static Date parseDate(String pDateString, String pDateFormat) {
        try {
            return new SimpleDateFormat(pDateFormat).parse(pDateString);
        } catch (ParseException ex) {
            cLogger.error("日期-格式不匹配！" + pDateString + ":" + pDateFormat, ex);
        }
        return null;
    }

    public static String getDateStr(Calendar pCalendar, String pFormat) {
        return new SimpleDateFormat(pFormat).format(pCalendar.getTime());
    }

    public static String date8to10(String pDate) {
        if ((null == pDate) || ("".equals(pDate))) {
            return pDate;
        }

        char[] mChars = pDate.toCharArray();

        return new StringBuilder().append(mChars, 0, 4).append('-').append(mChars, 4, 2).append('-').append(mChars, 6, 2).toString();
    }

    /**
     * @param num 天数
     * @return 距离当前日期num天的年月日
     */
    public static String getDate_AddDayNumStr(String date, Integer num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date t1 = null;
        try {
            t1 = sdf.parse(date);
        } catch (ParseException e) {
            cLogger.error("日期格式错误", e);
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(t1);
        calendar.add(Calendar.DAY_OF_MONTH, num);// 把日期往后减一天.整数往后推,负数往前移动
        return sdf.format(calendar.getTime());

    }

    public static Date getDate_AddDayNum(Integer num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date t1 = null;
        try {
            t1 = sdf.parse(String.valueOf(getCur8Date()));
        } catch (ParseException e) {
            cLogger.error("日期格式错误", e);
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(t1);
        calendar.add(Calendar.DAY_OF_MONTH, num);// 把日期往后减一天.整数往后推,负数往前移动
        return calendar.getTime();

    }

    /**
     * @param num 年数
     * @return 距离当前日期num年的年月日
     */
    public static String getDate_AddYearNum(String date, Integer num) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date t1 = null;
        try {
            t1 = sdf.parse(date);
        } catch (ParseException e) {
            cLogger.error("日期格式错误", e);
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(t1);
        calendar.add(Calendar.YEAR, num);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return sdf.format(calendar.getTime());

    }

    public static int getCur8Date() {
        return get8Date(Calendar.getInstance());
    }

    public static int get8Date(Calendar pCalendar) {
        return pCalendar.get(1) * 10000 + (pCalendar.get(2) + 1) * 100 + pCalendar.get(5);
    }

    public static String getFormatStr(String strDate, String pFormat) {
        SimpleDateFormat sf = new SimpleDateFormat(pFormat);
        try {
            return sf.format(sf.parse(strDate));
        } catch (ParseException e) {
            cLogger.error("格式化失败", e);
        }
        return null;
    }

    public static String get_YYYY_MM_DD(Date date) {
        if (date == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    public static String get_HH_mm_ss(Date date) {
        if (date == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 获取时间差（秒级）
     *
     * @param beginDate 起始时间
     * @return
     */
    public static long getDateSubSecond(String beginDate) {
        SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long between = 0;
        try {
            Date begin = dfs.parse(beginDate);
            Date end = new Date();
            between = (end.getTime() - begin.getTime()) / 1000;//除以1000是为了转换成秒
        } catch (ParseException e) {
            cLogger.error("格式化异常");
            e.printStackTrace();
        }
        return between;
    }

    /**
     * 获取时间差（秒级）
     *
     * @param beginDate 起始时间
     * @return
     */
    public static long getDateSubDay(Date beginDate, Date endDate) {
        long between = 0;
        try {
            between = (endDate.getTime() - beginDate.getTime()) / (1000 * 3600 * 24L);//除以1000是为了转换成秒
        } catch (Exception e) {
            cLogger.error("计算异常");
            e.printStackTrace();
        }
        return between;
    }


    /**
     * 获取时间格式 yyyy年MM月dd日
     */
    public static String get_YYYY_MM_DD_Chinese(Date date) {
        if (date == null)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * @param num 天数
     * @return 距离当前日期num天的年月日
     */
    public static Date getDate_AddDayNumStr(Date date, Integer num) {
        if (date == null)
            return null;
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, num);// 把日期往后减一天.整数往后推,负数往前移动
        return calendar.getTime();

    }

    /**
     * 获取 yyyyMMdd 格式日期
     *
     * @return
     */
    public static String get_yyyyMMdd() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        return formatter.format(currentTime);
    }

    /**
     * 获取HHmmss日期
     *
     * @return
     */
    public static String get_HHmmss() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
        return formatter.format(currentTime);
    }


}